require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test all units and functionals'
task :test do
  exceptions = ["test:units", "test:functionals"].collect do |task|
    begin
      Rake::Task[task].invoke
      nil
    rescue => e
      e
    end
  end.compact
  
  exceptions.each {|e| puts e;puts e.backtrace }
  raise "Test failures" unless exceptions.empty?
end

desc 'Generate documentation for the acts_as_replica plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
  rdoc.rdoc_dir = 'rdoc'
  rdoc.title    = 'ActsAsReplica'
  rdoc.options << '--line-numbers' << '--inline-source'
  rdoc.rdoc_files.include('README')
  rdoc.rdoc_files.include('lib/**/*.rb')
end

namespace :test do
  Rake::TestTask.new(:units) do |t|
    t.libs << 'test'
    t.libs << 'lib'
    t.pattern = 'test/unit/**/*_test.rb'
    t.verbose = true
  end
  Rake::Task['test:units'].comment = "Run the unit tests in test/unit"

  Rake::TestTask.new(:functionals) do |t|
    t.libs << 'test'
    t.libs << 'lib'
    t.pattern = 'test/functional/**/*_test.rb'
    t.verbose = true
  end
  Rake::Task['test:functionals'].comment = "Run the functional tests in test/functional"
end

namespace :rcov do
  desc "runs all the rcov individual test suite coverages"
  task :all do
    test_dir    = "#{Dir.pwd}/test"
    dirs        = [ "#{test_dir}/functional/*.rb",
                    "#{test_dir}/unit/*.rb"]
    command = "rcov -I#{Dir.pwd}/test/app_root -o #{test_dir}/coverage"
    dirs.each do |dir|
      command += " #{dir}" unless Dir[dir].empty?
    end
    system command
  end
end